{% extends 'index.html' %}
{% block subtitle %}- 注册页{% endblock %}

<!--head-->
{% block head %}
    <script type="text/javascript">
     function checkpassword(){
         var username = document.getElementById("username").value
         var password = document.getElementById("password").value;
         var repeatpassword = document.getElementById("repeatpassword").value;
         if (username === ""){
             alert("账号不能为空")
         }
         else if (username.length > 8) {
              alert("用户名只能为1-8个字符/汉字的长度")
          }
         else if (password === ""){
             alert("密码不能为空")
         }
         else if (username.length > 12) {
              alert("密码只能为1-12个字符/汉字的长度")
          }
         else if (repeatpassword === ""){
             alert("请填写确认密码")
         }
         else if (password!== repeatpassword){
             alert("两次密码不一致")
         }
         else{
            var data= {
                data: JSON.stringify({
                "username": username,
                "password": password,
            }),
            }
            $.ajax({
            url:"/register/",
            type:"post",
            data:data,
            datatype:"json",
            success:function(data){
                if (data['code'] === 200)
                {
                    alert("注册成功")
                    alert("系统测试期间，每个用户初始金额为1000！（虽然并不能买实际物品就是了^-^）")
                    window.location.href = "{{ url_for('main.index') }}"
                }
                else if(data['code'] ===402)
                    {
                    alert("该账号名已被注册，请换一个账号名")
                    }
            },
            error:function(error){
                console.log(error)
                alert("出现未知错误")
            }
            })
         }
     }
    </script>
{% endblock %}

{% block content %}
    <div class="login_banner">
        <div id="content">
            <div class="login_form">
                <div class="login_box">
                    <div class="tit">
                        <h1>注册</h1>
                        <span class="errorMsg"></span>
                    </div>
                    <div class="form">
                        <form>
                            <label>用户名称：</label>
                            <input class="itxt" type="text" placeholder="请输入用户名" autocomplete="off" tabindex="1" name="username" id="username" />
                            <br />
                            <br />
                            <label>注册密码：</label>
                            <input class="itxt" type="password" placeholder="请输入密码" autocomplete="off" tabindex="1" name="password" id="password" />
                            <br />
                            <br />
                            <label>确认密码：</label>
                            <input class="itxt" type="password" placeholder="确认密码" autocomplete="off" tabindex="1" name="repwd" id="repeatpassword" />
                            <br />
                            <br />
                            <input type="button" class = "input_button" value="注册"  id="sub_btn" onclick="checkpassword()">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
